{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e2b55d12",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy\n",
    "import pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e75b88e1",
   "metadata": {},
   "source": [
    "# DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64a0fa0b",
   "metadata": {},
   "source": [
    "DataFrame是一个【表格型】的数据结构，可以看作是【由Series组成的字典】。DataFrame由按照一定顺序排列的多列数据组成。\n",
    "**设计初衷是将Series的使用场景扩展到多维，DataFrame既有行索引，又有列索引**\n",
    "- 行索引 index\n",
    "- 列索引 columns\n",
    "- 值 value（Numpy的二维数组）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d9a659f",
   "metadata": {},
   "source": [
    "### 1 DataFrame的创建"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93faa107",
   "metadata": {},
   "source": [
    "- 最常用的方法是传递一个字典来创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "70d8b1b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>千峰</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Python</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Pandas</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     name  age\n",
       "0      千峰   11\n",
       "1  Python   30\n",
       "2  Pandas   20"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "Index(['name', 'age'], dtype='object')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {\n",
    "    'name':['千峰','Python','Pandas'],\n",
    "    'age':[11,30,20],\n",
    "}\n",
    "df = pandas.DataFrame(d)\n",
    "display(df)\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8723ae8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>物理</th>\n",
       "      <th>化学</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>小明</th>\n",
       "      <td>20</td>\n",
       "      <td>31</td>\n",
       "      <td>84</td>\n",
       "      <td>77</td>\n",
       "      <td>74</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小红</th>\n",
       "      <td>96</td>\n",
       "      <td>95</td>\n",
       "      <td>87</td>\n",
       "      <td>79</td>\n",
       "      <td>75</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小华</th>\n",
       "      <td>69</td>\n",
       "      <td>15</td>\n",
       "      <td>12</td>\n",
       "      <td>86</td>\n",
       "      <td>45</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小志</th>\n",
       "      <td>33</td>\n",
       "      <td>82</td>\n",
       "      <td>31</td>\n",
       "      <td>64</td>\n",
       "      <td>54</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语  物理  化学  生物\n",
       "小明  20  31  84  77  74  24\n",
       "小红  96  95  87  79  75  13\n",
       "小华  69  15  12  86  45  63\n",
       "小志  33  82  31  64  54  71"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df = pandas.DataFrame(\n",
    "    data=numpy.random.randint(10,100,size=(4,6)),\n",
    "    index=['小明','小红','小华','小志'],\n",
    "    columns=['语文','数学','英语','物理','化学','生物']\n",
    ")\n",
    "display(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bfdd3fe",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3bec960c",
   "metadata": {},
   "source": [
    "### 2 DataFrame的索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b35017b",
   "metadata": {},
   "source": [
    "- 对列进行索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2f4611c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "小明    20\n",
       "小红    96\n",
       "小华    69\n",
       "小志    33\n",
       "Name: 语文, dtype: int32"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "小明    20\n",
       "小红    96\n",
       "小华    69\n",
       "小志    33\n",
       "Name: 语文, dtype: int32"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>小明</th>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小红</th>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小华</th>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小志</th>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文\n",
       "小明  20\n",
       "小红  96\n",
       "小华  69\n",
       "小志  33"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(df.语文) # 得到的是Series类型\n",
    "display(df['语文']) # 得到的是Series类型\n",
    "\n",
    "display(df[['语文']]) # 得到的是DataFrame类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c982794",
   "metadata": {},
   "source": [
    "- 对行索引【需要使用loc[]和iloc[]函数】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "427ea96c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "语文    20\n",
       "数学    31\n",
       "英语    84\n",
       "物理    77\n",
       "化学    74\n",
       "生物    24\n",
       "Name: 小明, dtype: int32"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "语文    20\n",
       "数学    31\n",
       "英语    84\n",
       "物理    77\n",
       "化学    74\n",
       "生物    24\n",
       "Name: 小明, dtype: int32"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>物理</th>\n",
       "      <th>化学</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>小明</th>\n",
       "      <td>20</td>\n",
       "      <td>31</td>\n",
       "      <td>84</td>\n",
       "      <td>77</td>\n",
       "      <td>74</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小志</th>\n",
       "      <td>33</td>\n",
       "      <td>82</td>\n",
       "      <td>31</td>\n",
       "      <td>64</td>\n",
       "      <td>54</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语  物理  化学  生物\n",
       "小明  20  31  84  77  74  24\n",
       "小志  33  82  31  64  54  71"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(df.loc['小明']) # 返回的是Series类型\n",
    "display(df.iloc[0])\n",
    "\n",
    "display(df.loc[['小明','小志']]) # 返回的是DataFrame类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c72ac9e",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05696fe8",
   "metadata": {},
   "source": [
    "### 3 DataFrame的切片"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b627affc",
   "metadata": {},
   "source": [
    "**【注意】直接用中括号时：**\n",
    "- 索引优先对列进行操作\n",
    "- 切片优先对行进行操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3fc58897",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "26ba1ee9",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0835633f",
   "metadata": {},
   "source": [
    "### 4 DataFrame的运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e336676f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>小明</th>\n",
       "      <td>11</td>\n",
       "      <td>76</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小红</th>\n",
       "      <td>25</td>\n",
       "      <td>13</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小黄</th>\n",
       "      <td>15</td>\n",
       "      <td>70</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语\n",
       "小明  11  76  37\n",
       "小红  25  13  12\n",
       "小黄  15  70  32"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pandas.DataFrame(\n",
    "    data=numpy.random.randint(10,100,size=(3,3)),\n",
    "    index=['小明','小红','小黄'],\n",
    "    columns=['语文','数学','英语']\n",
    ")\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "64f6c9ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "语文    100\n",
       "数学     10\n",
       "英语      1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pandas.Series(data=[100,10,1],index=df1.columns)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "494113d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>小明</th>\n",
       "      <td>111</td>\n",
       "      <td>86</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小红</th>\n",
       "      <td>125</td>\n",
       "      <td>23</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小黄</th>\n",
       "      <td>115</td>\n",
       "      <td>80</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     语文  数学  英语\n",
       "小明  111  86  38\n",
       "小红  125  23  13\n",
       "小黄  115  80  33"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 + s"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
